﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace AzureStorageLight
{
    public static class StorageItemUtil
    {
        public static string GetParent(string path)
        {
            if (string.IsNullOrEmpty(path) || path == "/") return null;
            path = path.TrimEnd('/');
            var parent = path.Substring(0, path.LastIndexOf("/"));
            if (string.IsNullOrEmpty(parent)) return "/";
            return parent + "/";
        }
        public static string GetFileName(string path)
        {
            if (string.IsNullOrEmpty(path) || path == "/") return "/";
            path = path.TrimEnd('/');
            return path.Substring(path.LastIndexOf("/") + 1);
        }
        public static string Md5HashStringForUtf8String(string s)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(s);

            MD5 md5 = MD5.Create();
            byte[] hashBytes = md5.ComputeHash(bytes);

            return HexStringFromBytes(hashBytes);
        }
        public static string HexStringFromBytes(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string hex in bytes.Select(b => b.ToString("x2")))
                sb.Append(hex);
            return sb.ToString();
        }

    }

}
